package com.mysoft.gfyx.clue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.framework.common.util.MappingUtil;
import com.mysoft.framework.context.api.ContextManager;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.clue.controller.dto.DeleteByIdRequestDto;
import com.mysoft.gfyx.clue.dao.ClueDao;
import com.mysoft.gfyx.clue.dao.CluecontacterDao;
import com.mysoft.gfyx.clue.dao.entity.Clue;
import com.mysoft.gfyx.clue.dao.entity.Cluecontacter;
import com.mysoft.gfyx.clue.dao.entity.Cluefollowrecord;
import com.mysoft.gfyx.clue.service.CluecontacterService;
import com.mysoft.gfyx.clue.service.dto.ClueContacterDto;
import com.mysoft.gfyx.clue.service.dto.ClueDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 描述：Cluecontacter 业务代码
 *
 * @author JavaSdk generator
 * @date 2022-07-07
 */
@Service
public class CluecontacterServiceImp extends AppService implements CluecontacterService {

    @Autowired
    private CluecontacterDao cluecontacterDao;

    @Autowired
    private ClueDao clueDao;

    @Autowired
    private ContextManager contextManager;

    public List<ClueContacterDto> getClueContactersByClueId(UUID clueId) {
        List<ClueContacterDto> dtos = new ArrayList<>();

        LambdaQueryWrapper<Cluecontacter> lqw = Wrappers.lambdaQuery();
        lqw.eq(Cluecontacter::getClueGUID, clueId);
        List<Cluecontacter> list = cluecontacterDao.selectList(lqw);

        list.forEach(c -> {
            ClueContacterDto dto = MappingUtil.getSimpleMapperFacade().map(c, ClueContacterDto.class);
            dtos.add(dto);
        });

        return dtos;
    }

    public ClueContacterDto getClueContacterByClueTel(UUID clueId, String tel) {
        LambdaQueryWrapper<Cluecontacter> lqw = Wrappers.lambdaQuery();
        lqw.eq(Cluecontacter::getClueGUID, clueId);
        lqw.eq(Cluecontacter::getCompanyTel, tel);
        lqw.last("limit 1");

        Cluecontacter clueContacter = cluecontacterDao.selectOne(lqw);
        if (clueContacter == null) {
            return null;
        } else {
            return MappingUtil.getSimpleMapperFacade().map(clueContacter, ClueContacterDto.class);
        }
    }

    public void addClueContactor(ClueContacterDto clueContacterDto) {
        if (clueContacterDto.getCompanyTel() == null || clueContacterDto.getCompanyTel().equals("")) {
            throw new BusinessLogicException("该联系人手机号不能为空");
        }
        Cluecontacter clueContacter = MappingUtil.getSimpleMapperFacade().map(clueContacterDto, Cluecontacter.class);
        QueryWrapper<Cluecontacter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ClueGUID", clueContacter.getClueGUID());
        queryWrapper.eq("CompanyTel", clueContacter.getCompanyTel());
        Long isExist = cluecontacterDao.selectCount(queryWrapper);
        if (isExist > 0) {
            throw new BusinessLogicException("该联系人手机号已存在，添加失败。");
        }
        cluecontacterDao.insert(clueContacter);
    }

    public void updateClueContactor(ClueContacterDto clueContacterDto) {
        Cluecontacter clueContacter = MappingUtil.getSimpleMapperFacade().map(clueContacterDto, Cluecontacter.class);
        QueryWrapper<Cluecontacter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ClueGUID", clueContacter.getClueGUID());
        queryWrapper.eq("CompanyTel", clueContacter.getCompanyTel());
        queryWrapper.last("limit 1");
        Cluecontacter isExist = cluecontacterDao.selectOne(queryWrapper);
        if (isExist != null && !clueContacter.getClueContacterGUID().equals(isExist.getClueContacterGUID())) {
            throw new BusinessLogicException("该联系人手机号已存在，修改失败。");
        }
        cluecontacterDao.updateById(clueContacter);
    }

    public void deleteClueContactor(DeleteByIdRequestDto dto) {
        QueryWrapper<Cluecontacter> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("ClueContacterGUID", dto.getId());
        deleteWrapper.last("limit 1");
        Cluecontacter cluContact = cluecontacterDao.selectOne(deleteWrapper);
        if (cluContact == null) {
            throw new BusinessLogicException("操作失败，未找到相关记录");
        } else {
            //先判断当前操作人是否有线索的权限，若有，才能删除线索下的联系人
            QueryWrapper<Clue> deleteWrapperClue = new QueryWrapper<>();
            UUID userId = contextManager.fetchContextInfo().getUserContext().getUserId();
            deleteWrapperClue.eq("ClueGUID", cluContact.getClueGUID());
            deleteWrapperClue.eq("ClueResponsiblePersonGUID", userId);
            Long isExist = clueDao.selectCount(deleteWrapperClue);
            if (isExist < 1) {
                throw new BusinessLogicException("操作失败，无操作权限");
            } else {//删除线索联系人
                cluecontacterDao.delete(deleteWrapper);
            }
        }
    }

    public ClueContacterDto getClueContactersById(UUID id) {
        QueryWrapper<Cluecontacter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ClueContacterGUID", id);
        queryWrapper.last("limit 1");
        Cluecontacter cluContact = cluecontacterDao.selectOne(queryWrapper);
        return MappingUtil.getSimpleMapperFacade().map(cluContact, ClueContacterDto.class);
    }
}
